<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>九宫格裁剪工具 - 在线工具集合</title>
    <script src="../utils/toast.js"></script>
    <link rel="stylesheet" href="../utils/styles.css">
    <style>
        @import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap');

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Poppins', sans-serif;
            background: linear-gradient(135deg, #0f0c29, #302b63, #24243e);
            min-height: 100vh;
            color: #fff;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 40px 20px;
        }

        .header {
            text-align: center;
            margin-bottom: 40px;
        }

        .header h1 {
            font-size: 2.5em;
            margin-bottom: 10px;
            background: linear-gradient(45deg, #00ff88, #00b4d8);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
        }

        .header p {
            color: rgba(255, 255, 255, 0.8);
            font-size: 1.1em;
        }

        .upload-area {
            border: 2px dashed rgba(255, 255, 255, 0.2);
            border-radius: 20px;
            padding: 40px;
            text-align: center;
            cursor: pointer;
            transition: all 0.3s ease;
            margin-bottom: 30px;
            background: rgba(255, 255, 255, 0.05);
        }

        .upload-area:hover {
            border-color: #00ff88;
            background: rgba(255, 255, 255, 0.1);
        }

        .preview-container {
            display: grid;
            grid-template-columns: repeat(3, 1fr);
            gap: 10px;
            margin: 30px 0;
            max-width: 900px;
            margin: 0 auto;
        }

        .grid-item {
            position: relative;
            width: 100%;
            padding-bottom: 100%;
            background: rgba(0, 0, 0, 0.2);
            border-radius: 8px;
            overflow: hidden;
        }

        .grid-item img {
            position: absolute;
            width: 100%;
            height: 100%;
            object-fit: cover;
        }

        .action-buttons {
            display: flex;
            gap: 20px;
            justify-content: center;
            margin-top: 30px;
        }

        .action-btn {
            padding: 15px 30px;
            border: none;
            border-radius: 12px;
            background: linear-gradient(45deg, #00ff88, #00b4d8);
            color: #1a1a1a;
            font-size: 1.1em;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
        }

        .action-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(0, 255, 136, 0.3);
        }

        .back-link {
            display: inline-block;
            margin-bottom: 20px;
            color: #00ff88;
            text-decoration: none;
            font-weight: 500;
            transition: all 0.3s ease;
        }

        .back-link:hover {
            transform: translateX(-5px);
        }

        @media (max-width: 768px) {
            .preview-container {
                grid-template-columns: repeat(3, 1fr);
                gap: 5px;
            }

            .action-buttons {
                flex-direction: column;
            }

            .action-btn {
                width: 100%;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <a href="../index.html" class="back-link">← 返回首页</a>
        
        <div class="header">
            <h1>九宫格裁剪工具</h1>
            <p>将图片裁剪成九宫格样式，适合社交媒体发布</p>
        </div>

        <div class="upload-area" id="uploadArea">
            <div class="upload-text">点击或拖拽图片到这里</div>
            <input type="file" id="imageInput" accept="image/*" style="display: none;">
        </div>

        <div class="preview-container" id="previewContainer">
            <div class="grid-item"></div>
            <div class="grid-item"></div>
            <div class="grid-item"></div>
            <div class="grid-item"></div>
            <div class="grid-item"></div>
            <div class="grid-item"></div>
            <div class="grid-item"></div>
            <div class="grid-item"></div>
            <div class="grid-item"></div>
        </div>

        <div class="action-buttons">
            <button class="action-btn" id="generateBtn">生成九宫格</button>
            <button class="action-btn" id="downloadBtn">下载全部</button>
        </div>
    </div>

    <script>
        const uploadArea = document.getElementById('uploadArea');
        const imageInput = document.getElementById('imageInput');
        const previewContainer = document.getElementById('previewContainer');
        const generateBtn = document.getElementById('generateBtn');
        const downloadBtn = document.getElementById('downloadBtn');
        let originalImage = null;

        // 文件处理
        uploadArea.addEventListener('click', () => imageInput.click());
        uploadArea.addEventListener('dragover', (e) => {
            e.preventDefault();
            uploadArea.style.borderColor = '#00ff88';
        });
        uploadArea.addEventListener('dragleave', () => {
            uploadArea.style.borderColor = 'rgba(255, 255, 255, 0.2)';
        });
        uploadArea.addEventListener('drop', (e) => {
            e.preventDefault();
            const file = e.dataTransfer.files[0];
            if (file && file.type.startsWith('image/')) {
                handleImage(file);
            }
        });

        imageInput.addEventListener('change', (e) => {
            const file = e.target.files[0];
            if (file) {
                handleImage(file);
            }
        });

        function handleImage(file) {
            const reader = new FileReader();
            reader.onload = (e) => {
                originalImage = new Image();
                originalImage.onload = () => {
                    generateGridImages();
                };
                originalImage.src = e.target.result;
            };
            reader.readAsDataURL(file);
        }

        function generateGridImages() {
            if (!originalImage) {
                toast.error('请先上传图片');
                return;
            }

            const gridItems = document.querySelectorAll('.grid-item');
            const size = Math.min(originalImage.width, originalImage.height);
            const partSize = size / 3;
            
            gridItems.forEach((item, index) => {
                const canvas = document.createElement('canvas');
                const ctx = canvas.getContext('2d');
                const row = Math.floor(index / 3);
                const col = index % 3;

                canvas.width = partSize;
                canvas.height = partSize;

                // 计算裁剪位置
                const sx = (originalImage.width - size) / 2 + col * partSize;
                const sy = (originalImage.height - size) / 2 + row * partSize;

                ctx.drawImage(originalImage, sx, sy, partSize, partSize, 0, 0, partSize, partSize);

                const img = document.createElement('img');
                img.src = canvas.toDataURL('image/jpeg', 0.9);
                
                // 清除之前的图片
                while (item.firstChild) {
                    item.removeChild(item.firstChild);
                }
                item.appendChild(img);
            });

            toast.success('九宫格生成成功');
        }

        generateBtn.addEventListener('click', generateGridImages);

        downloadBtn.addEventListener('click', () => {
            if (!originalImage) {
                toast.error('请先上传图片');
                return;
            }

            const gridItems = document.querySelectorAll('.grid-item img');
            let downloadCount = 0;

            gridItems.forEach((img, index) => {
                const link = document.createElement('a');
                link.download = `grid_${index + 1}.jpg`;
                link.href = img.src;
                link.click();
                downloadCount++;
            });

            toast.success(`开始下载 ${downloadCount} 张图片`);
        });
    </script>
</body>
</html> 